package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.util.DataUtil;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.domain.dao.FundTransferDao;
import com.xbongbong.saas.domain.entity.FundTransferEntity;
import com.xbongbong.saas.model.FundTransferModel;
import com.xbongbong.saas.model.FundTransferSubDataModel;
import com.xbongbong.saas.model.PaasFormSubDataModel;
import org.elasticsearch.action.support.WriteRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author bomfish
 */
@Service("fundTransferModel")
public class FundTransferModelImpl implements FundTransferModel {
    @Resource
    private FundTransferDao fundTransferDao;
    @Resource
    private PaasEsModel paasEsModel;
    @Resource
    private PaasFormSubDataModel paasFormSubDataModel;
    @Resource
    private FundTransferSubDataModel fundTransferSubDataModel;

    @Override
    public Integer insert(FundTransferEntity entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);

        paasFormSubDataModel.transferSubForm(entity, IndexTypeEnum.IDX_SAAS_FUND_TRANSFER_SUB);
        Integer insert = fundTransferDao.insert(entity);
        InsertDTO insertDTO = paasEsModel.getSaveDTO(entity, IndexTypeEnum.IDX_SAAS_FUND_TRANSFER);
        paasEsModel.insert(insertDTO);

        paasFormSubDataModel.insertSubFormData(entity, IndexTypeEnum.IDX_SAAS_FUND_TRANSFER_SUB);
        return insert;
    }

    @Override
    public Integer update(FundTransferEntity entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);

        paasFormSubDataModel.transferSubForm(entity, IndexTypeEnum.IDX_SAAS_FUND_TRANSFER_SUB);
        Integer integer = fundTransferDao.update(entity);
        Integer updateSub = paasFormSubDataModel.updateSubFormData(entity, IndexTypeEnum.IDX_SAAS_FUND_TRANSFER_SUB);
        if (Objects.equals(1, integer) || Objects.equals(1, updateSub)) {
            // 更新时间
            integer = fundTransferDao.updateTime(now, entity.getId(), entity.getCorpid());
            // 更新es
            PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
            BeanUtil.copyProperties(entity, paasFormDataEntity, false);
            InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_FUND_TRANSFER);
            insertDTO.getSource().remove("creatorId");
            paasEsModel.update(insertDTO);

        }
        if (Objects.nonNull(entity.getSubFormData()) && !entity.getSubFormData().isEmpty()) {
            entity.getData().putAll(entity.getSubFormData());
        }
        return integer;
    }

    @Override
    public List findEntitys(Map<String, Object> param) {
        List<FundTransferEntity> entitys = fundTransferDao.findEntitys(param);
        Object corpid = param.get(ParameterConstant.CORPID);
        if (Objects.nonNull(corpid)) {
            paasFormSubDataModel.findSubEntity(corpid.toString(), entitys, IndexTypeEnum.IDX_SAAS_FUND_TRANSFER_SUB);
        }
        return entitys;
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return fundTransferDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return fundTransferDao.findEntitys(param);
    }

    @Override
    public FundTransferEntity getByKey(Long key, String corpid) {
        FundTransferEntity entity = fundTransferDao.getByKey(key, corpid);
        paasFormSubDataModel.findSubEntity(corpid, Collections.singletonList(entity), IndexTypeEnum.IDX_SAAS_FUND_TRANSFER_SUB);
        return entity;
    }

    @Override
    public Integer updateBatch(List<UpdateDataEntity> updateList, String corpid) throws XbbException {
        //1. 先拿到子表单数据
        List<UpdateDataEntity> subList = DataUtil.getUpdateSubDataList(updateList);
        // 2. 主表删掉子表单数据
        DataUtil.removeSubDataInData(updateList);
        Integer integer = 0;
        if (CollectionsUtil.isNotEmpty(updateList)) {
            integer = fundTransferDao.updateBatch(updateList, corpid);
            paasEsModel.updateBatchMuchField(updateList, IndexTypeEnum.IDX_SAAS_FUND_TRANSFER);
        }
        if (!subList.isEmpty()) {
            fundTransferSubDataModel.updateBatch(subList, corpid);
        }
        return integer;
    }
    @Override
    public Integer updateBatch(List<UpdateDataEntity> updateList, String corpid, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException {
        //1. 先拿到子表单数据
        List<UpdateDataEntity> subList = DataUtil.getUpdateSubDataList(updateList);
        // 2. 主表删掉子表单数据
        DataUtil.removeSubDataInData(updateList);
        Integer integer = 0;
        if (CollectionsUtil.isNotEmpty(updateList)) {
            integer = fundTransferDao.updateBatch(updateList, corpid);
            paasEsModel.updateBatchMuchField(updateList, IndexTypeEnum.IDX_SAAS_FUND_TRANSFER, refreshPolicy);
        }
        if (!subList.isEmpty()) {
            fundTransferSubDataModel.updateBatch(subList, corpid);
        }
        return integer;
    }

    @Override
    public void deleteBatch(List<Long> ids, String corpid) throws XbbException {
        fundTransferDao.deleteBatch(ids, corpid);
        paasEsModel.deleteBatch(ids, corpid, DelEnum.DELETE.getDel(), IndexTypeEnum.IDX_SAAS_FUND_TRANSFER);
        paasFormSubDataModel.deleteBatchByDataId(ids, corpid, IndexTypeEnum.IDX_SAAS_FUND_TRANSFER_SUB, DelEnum.DELETE_SUBFORM);
    }
}
